<?php

namespace Application\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Expression;

class ThematiquesTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
    	$this->tableGateway = $tableGateway;
    }
    
    public function fetchAll()
    {
        $resultSet = $this->tableGateway->select(function(Select $select){
            $select->order('themeLibelle ASC');
        });
        
        return $resultSet;
    }

    /* retourne toutes les thematiques utilisées par des séjours */
    public function fetchAllUsed()
    {
        $sql = new Sql($this->tableGateway->getAdapter());
        $select = $sql->select();

        $select->from($this->tableGateway->table)
                ->quantifier('DISTINCT')
                ->join('ThemesSejour', 'ThemesSejour.themeId = Thematiques.themeId', array(), 'inner')
                ->order('themeLibelle ASC');
        
        $statement = $sql->prepareStatementForSqlObject($select);
        $resultSet = $statement->execute();

        return $this->toArray($resultSet);
    }

    public function toArray ($resultSet)
    {
        $entities = array();
        
        foreach ($resultSet as $row) {
            $obj = new Thematiques();
            $obj->exchangeArray( (array) $row);
            $entities[] = $obj;
        }
        
        return $entities;
    }
    
    public function fetchAllArray()
    {
        $resultSet = $this->fetchAll();
        $entities = array();
        
        foreach ($resultSet as $row) {
        	$obj = new Thematiques();
        	$obj->exchangeArray( (array) $row);
        	$entities[] = $obj;
        }
        
        return $entities;
    }

}